package com.bafa.objects;

import java.io.Serializable;
import java.util.*;

import com.bafa.dbcache.LeagueCache;
import com.bafa.front.utils.FrontUtils;
import com.bafa.objects.schema.ObjectFactory;
import com.bafa.services.RetrieveServices;
import com.bafa.util.WebsiteUtils;

public class Score extends Match implements Serializable{

	

	private int matchId;
	private int seasonId;
	private boolean weAdded = false;
	private boolean oppoAdded = false;
	private Match match;
	private boolean homeTeam;
	
	public Score(com.bafa.objects.schema.Score score){
		System.out.println("SCORE ID: "+score.getID());
		matchId = score.getID();
		match = RetrieveServices.getMatch(matchId);
		homeTeam = score.isHome();
		weAdded = (score.isHome())?match.isHomeAdded():match.isAwayAdded();
		oppoAdded = (!score.isHome())?match.isAwayAdded():match.isHomeAdded();
		if(this.oppoAdded&&this.weAdded) this.confirmed=true;
		this.date=match.getDate();
		this.seasonId=match.getSeasonId();
		this.time=match.getTime();
		this.confirmed = match.isConfirmed();
	}
	
public Score(Match score, boolean home){
		
		matchId = score.getID();
		match = score;
		
                homeTeam = home;
		weAdded = (home)?match.isHomeAdded():match.isAwayAdded();
		oppoAdded = (!home)?match.isAwayAdded():match.isHomeAdded();
		if(this.oppoAdded&&this.weAdded) this.confirmed=true;
		this.date=match.getDate();
		this.seasonId=match.getSeasonId();
		this.time=match.getTime();
		this.confirmed = match.isConfirmed();
	}

	public int getSeasonId() {
		return seasonId;
	}
	
	public int getMatchId() {
		return matchId;
	}
	public HashMap getScorers() {
		if(homeTeam){
			return match.getScorersA();
		}else{
			return match.getScorersB();
		}
	}
	public Score(boolean home, int matchId,Date date, int oppoId, int goalsFor, int goalsAgainst, int played,int seasonId,int blocked,int walkover, String time,int division,int oppoAdded, int weAdded) {
		super();
		this.matchId=matchId;
		this.date=date;
		
		this.homeTeam=home;
		
		this.seasonId=seasonId;
		if(played>0)this.played=true;
		if(blocked>0)this.blocked=true;
		if(walkover>0)this.walkover=true;
		
		this.time=time;
		this.division = division;
		
		if(oppoAdded>0) this.oppoAdded = true;
		if(weAdded>0) this.weAdded=true;
		if(this.oppoAdded&&this.weAdded) this.confirmed=true;
		
		
	}
	public boolean isDraw() {
		if(getGoalsFor()==getGoalsAgainst())return true;
		else return false;
	}
	public int getGoalsAgainst() {
		if(homeTeam){
			return match.getGoalsB();
		}else{
			return match.getGoalsA();
		}
	}
	public int getGoalsFor() {
		if(homeTeam){
			return match.getGoalsA();
		}else{
			return match.getGoalsB();
		}
	}
	public int getOppoId() {
		if(homeTeam){
			return match.getTeamB();
		}else{
			return match.getTeamA();
		}
	}
	public String getOpposition() {
		if(homeTeam){
			return match.getTeamBName();
		}else{
			return match.getTeamAName();
		}
	}
	public boolean isWon() {
		if(getGoalsFor()>getGoalsAgainst())return true;
		else return false;
	}
	
	
	
	public String[] scorers(){
		if(homeTeam){
			return match.scorersA();
		}else{
			return match.scorersB();
		}
	}
	
	public boolean isPlayed() {
		return match.isPlayed();
	}
	
	
	public boolean isResultDue(){
		Date today = Calendar.getInstance().getTime();
		Date matchDate = match.getDate();
		
		boolean played = match.isPlayed();
		boolean blocked = match.isBlocked();
		return matchDate.before(today)&&!isWeAdded();
	}
	
	
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("GoalsFor "+getGoalsFor());
		buffer.append("\nGoalsAgainst "+getGoalsAgainst());
		return buffer.toString();
	}
	public static void main(String[] args) {
	//	Score score = new Score(1,Calendar.getInstance(),1,2,1,1,1,1,0);
		
		//System.out.println(score.isResultDue());
		
	}

	public boolean isOppoAdded() {
		return oppoAdded;
	}

	public boolean isWeAdded() {
		return weAdded;
	}
	
	@Override
	public boolean equals(Object arg0) {
		int id = ((Integer)arg0).intValue();
		
		return this.getMatchId()==id;
	}
	
	public com.bafa.objects.schema.Score mapTo(){
		ObjectFactory factory = new ObjectFactory();
		com.bafa.objects.schema.Score score = factory.createScore();
		score.setHome(homeTeam);
		score.setID(matchId);
		
		return score;
	}
}
